* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file sets up the dataset used for the regression analysis of export market outcomes with the synthetic merger control group.

foreach v in 3c 4c 9c{

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exp_mergers_synthetic_02102023_v`v'.dta", clear 

* Make each event into panel, ensure all years are in firm-level data
drop if aar < 1996 | aar > 2015
keep cvrnrA aar merger
duplicates drop
expand 7
bysort cvrnrA aar: gen d_yr = _n - 4
gen aarA = aar + d_yr

* Merge with acquirer firm characteristics
rename cvrnrA cvrnr
rename aar aar_ori
rename aarA aar
merge 1:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", keepusing(aar employee_cvrnr CVR_BRANCHE_DB93 CVR_BRANCHE_DB07)

* Only keep acquirers in this sample, drop all other firms
gen acquirer = (aar_ori!=.)
egen maxacquirer = max(acquirer), by(cvrnr)
keep if maxacquirer==1

rename aar aarA
rename aar_ori aar
keep cvrnr aar d_yr aarA employee_cvrnr CVR_BRANCHE_DB07 merger
egen acquirer = max(merger), by(cvrnr)
drop merger

* Match to VARS data
rename cvrnr cvrnrA
merge 1:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_cvrnr.dta", keep(match master) nogen

foreach x in nprod nmark nprodmark vrdA max_vrdA med_vrdA min_vrdA{
	replace `x' = 0 if `x'==.
	}

gen lnnprod = ln(nprod)
gen lnnmark = ln(nmark)
gen lnnprodmark = ln(nprodmark)
gen lnvrdA = ln(vrdA)
gen lnrev_max=ln(max_vrdA)
gen lnrev_med=ln(med_vrdA)
gen lnrev_min=ln(min_vrdA)

* Indicator for each Indexvalue, except 0 (reference year)
foreach i of numlist 1(1)3{
gen m`i'=0
replace m`i'=1 if d_yr==-`i'
}

foreach i of numlist 1(1)3 {
gen p`i'=0
replace p`i'=1 if d_yr==`i'
}

gen test=0
egen sumvrdA_before = sum(vrdA) if d_yr<1, by(cvrnrA aar)
egen msumvrdA_before = mean(sumvrdA_before), by(cvrnrA aar)
gen exporter_before = (msumvrdA_before>0)

replace exporter_before = . if aar==.

gen ind=floor(CVR_BRANCHE_DB07/10000)

drop if aarA<1993

egen group_cvrnr = group(cvrnr)
compress
save "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_did_imputation_export_prepare_jc_synthetic_02102023_v`v'.dta", replace
}
